﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using EzDesk.DesktopClient.Event;
using EzDesk.DesktopClient.FunctionWindowModule.ViewModel;
using EzDesk.DesktopClient.FunctionWindowModule.View;
using EzDesk.EventAggregation;
using EzDesk.Generic;
using Microsoft.Practices.Unity;


namespace EzDesk.DesktopClient.FunctionWindowModule
{
    public class FunctionWindowModuleController
    {
        //private readonly List<IFunctionPageViewModel> _functionPageViewModelCollection;
        private readonly Dictionary<string, IFunctionPageViewModel> _functionPageViewModelCollection;
        private IActionViewModel _actionViewModel;
        private IUserManagement _userManagement;
        private UnityContainer _container;

        [InjectionConstructor]
        public FunctionWindowModuleController(IActionViewModel actionViewModel, IEnumerable<IFunctionPageViewModel> functionPageViews, IUserManagement userManagement,UnityContainer container)
        {
            FunctionNavigateEvent.Instance.Subscribe(this.OnFunctionNavigate);
            LogoutEvent.Instance.Subscribe(this.OnUserLogout);

            _actionViewModel = actionViewModel;

            _functionPageViewModelCollection = new Dictionary<string, IFunctionPageViewModel>();

            _userManagement = userManagement;
            _container = container;

            //ToDo:Performance Fix
            _functionPageViewModelCollection.Add("", _container.Resolve<IFunctionPageViewModel>());

        }

        public void OnFunctionNavigate(FunctionNavigateArgs args)
        {
            string targetValue = GetTargetType(args);

            //ActionRequestHandler(args);

            if (targetValue == "action")
                ActionRequestHandler(args);
            else
                PageRequestHandler(args);
        }

        protected void OnUserLogout(LogoutArgs args)
        {
            _functionPageViewModelCollection.Clear();
        }

        private string GetTargetType(FunctionNavigateArgs args)
        {
            var result = Regex.Match(args.Url, @"target=\w+");
            try
            {
                if (!result.Success) return "page";
                return result.Value.Split('=')[1];
            }
            catch (Exception)
            {
                return "page";
            }
        }

        private void ActionRequestHandler(FunctionNavigateArgs args)
        {
            string urlWithToken = _userManagement.GetUrlWithUserToken(args.Url);
            _actionViewModel.DoAction(urlWithToken);
        }

        private void PageRequestHandler(FunctionNavigateArgs args)
        {
            if (args.CallName == null) args.CallName = args.Url;
            

            if (!_functionPageViewModelCollection.ContainsKey(args.CallName) || _functionPageViewModelCollection[args.CallName]==null)
            {
                var viewModel = _container.Resolve<IFunctionPageViewModel>().SetCallName(args.CallName);
                _functionPageViewModelCollection[args.CallName] = viewModel;
                viewModel.OnCloseViewModel += viewModel_OnCloseViewModel;
            }

            IFunctionPageViewModel functionPageViewModel = _functionPageViewModelCollection[args.CallName];

            string urlWithToken = _userManagement.GetUrlWithUserToken(args.Url);
            LogEx.TRACE("FunctionWindowModule", "Navigate to: " + urlWithToken);
            functionPageViewModel.Navigate(urlWithToken);
        }

        void viewModel_OnCloseViewModel(object sender, EventArgs e)
        {
            var viewModel = sender as IFunctionPageViewModel;
            _functionPageViewModelCollection.Remove(viewModel.CallName);
        }
    }
}